package com.xuxueli.applyModules.utils;

import org.apache.commons.lang3.RandomStringUtils;

import javax.activation.MimetypesFileTypeMap;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @ClassName RandomUtil
 * @Description: TODO
 **/
public class RandomUtil {

    private static final int[] NUMBERS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    private static final String[] CHARS = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
        "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    private static final String[] SPECIALS = {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "+"};
    private static final int PASSWORD_LEN = 16;
    private static final int VERIFY_CODE_LEN = 6;
    private static final List<String> FILTER_CHAR = Arrays.asList("i", "I", "l", "L","0","o","O");

    /**
     * 16位密码
     */
    public static String password() {
        StringBuilder result = new StringBuilder();
        Random typeRandom = new Random();
        Random indexRandom = new Random();
        for (int index = 0; index < PASSWORD_LEN; index++) {
            int next = typeRandom.nextInt(4);
            switch (next) {
                case 0:
                    result.append(NUMBERS[indexRandom.nextInt(10)]);
                    break;
                case 1:
                    result.append(CHARS[indexRandom.nextInt(26)]);
                    break;
                case 2:
                    result.append(CHARS[indexRandom.nextInt(26)].toUpperCase());
                    break;
                case 3:
                    result.append(SPECIALS[indexRandom.nextInt(13)]);
                    break;
                default:
            }
        }
        return result.toString();
    }

    /**
     * 6位验证码
     * @return
     */
    public static String verifyCode() {
        StringBuilder result = new StringBuilder();
        Random typeRandom = new Random();
        Random indexRandom = new Random();
        String str;
        int index = 0;
        while (index < VERIFY_CODE_LEN) {
            int next = typeRandom.nextInt(3);
            switch (next) {
                case 1:
                    str = CHARS[indexRandom.nextInt(26)];
                    break;
                case 2:
                    str = CHARS[indexRandom.nextInt(26)].toUpperCase();
                    break;
                default:
                    str = NUMBERS[indexRandom.nextInt(10)] + "";
                    break;
            }
            if (FILTER_CHAR.contains(str)) {
                continue;
            }
            result.append(str);
            index++;
        }
        return result.toString();
    }

    /**
     * 随机数：包含数字
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 16位随机数：包含数字及字母
     * @return
     */
    public static String getNumRandom() {
        return RandomStringUtils.random(16,true,true);
    }

    public static void main(String[] args) {
        String fileName = "images.png";
        String type = new MimetypesFileTypeMap().getContentType(fileName);
        System.out.println("第二种javax.activation: "+type);
        System.out.println(RandomUtil.verifyCode());
    }
}
